#!/usr/bin/env python2
# vim:fileencoding=utf-8
# ebook-convert Desktop/阮一峰网络日志-周刊.recipe .epub --test -vv --debug-pipeline debug
from __future__ import unicode_literals, division, absolute_import

from calibre.web.feeds.news import BasicNewsRecipe

class ruanyifengblog(BasicNewsRecipe):

    # 延迟下载
    delay = 0
    # 下载线程数
    simultaneous_downloads = 5
    max_articles_per_feed = 300 # 抓取文章数量

    title = '观点与感想'
    __author__ = '阮一峰' # 作者
    description = '阮一峰的博客'
    timefmt = '[%Y-%m-%d]'
#    cover_url = 'http://iissnan.com/progit/assets/img/pro-git-cover.jpeg'

    url = 'http://www.ruanyifeng.com/blog/opinions/'
    no_stylesheets = True
    language = 'zh-CN'
    # 如果没有手动分析文章结构，可以考虑开启该选项自动清理正文内容
    # auto_cleanup = True

    # 仅保留文章的hentry中的内容，其中为自己分析得到的正文范围
    keep_only_tags = [{ 'class': 'hentry' }]

    def parse_index(self):
        soup = self.index_to_soup(self.url)

        divs = soup.findAll('div', {'class': 'module-categories module'})
        div = divs[0].findAll('ul', {'class': 'module-list'})

        articles = []
        for d in div:
            for link in d.findAll('a'):

                til = link.contents[0].strip()
                url = link['href']
                a = { 'title': til, 'url': url }
                articles.append(a)

#        return
#        articles.reverse()    # 文章倒序，让其按照时间从前到后排列
        # 章节
        ans = [(self.title, articles)]
# self.abort_recipe_processing('test')  # 用来中断电子书生成，调试用
        return ans
